#include "FilterSphere.h"
#include <stdio.h>
#include <cmath>

FilterSphere::FilterSphere(double support) : Filter(support)
{
	max_x_coord = 0.5 + (support - 1.0);
	max_y_coord = 0.5 + (support - 1.0);
	min_x_coord = -0.5 - (support - 1.0);
	min_y_coord = -0.5 - (support - 1.0);
	max_distance = max_x_coord * max_x_coord;
}

FilterSphere::~FilterSphere()
{}

double FilterSphere::getWeight(double x, double y, int num_samples) const
{
	if(x < min_x_coord || y < min_y_coord)
		return 0;
	if(x > max_x_coord || y > max_y_coord)
		return 0;
	return (std::sqrt((x*x) + (y*y))) / support;
}

void FilterSphere::getWeights(double* weights, int samples, double* xs, double* ys) const
{
	double total_weights = 0;
	for(int i = 0; i < samples; i++)
		{
			//printf("Values: %f\t%f\n", xs[i], ys[i]);
			//printf("Extremes: %f\t%f\t%f\t%f\n", max_x_coord, max_y_coord, min_x_coord, min_y_coord);
			double cur_weight = getWeight(xs[i], ys[i], samples);
			//printf("Weight: %f\n", cur_weight);
			weights[i] = cur_weight;
			total_weights += cur_weight;
		}
	for(int i = 0; i < samples; i++)
		{
			weights[i] = weights[i] / total_weights;
		}
}

